package com.example.designModel.工厂方法.计算器;

/**
 * XXXXXXXXX
 *
 * @author brad zhang
 * @version 2.0
 * @date 2021/11/17 16extends  46
 */
public class FactoryMethod {




    /// <summary>
    /// 减法类
    /// </summary>
    class OperationSub extends Operation {

        public double GetResult() {
            double result = 0;
            result = NumberA - NumberB;
            return result;
        }
    }

    /// <summary>
    /// 乘法类
    /// </summary>
    class OperationMul extends Operation {

        public double GetResult() {
            double result = 0;
            result = NumberA * NumberB;
            return result;
        }
    }

    /// <summary>
    /// 除法类
    /// </summary>
    class OperationDiv extends Operation {

        public double GetResult() throws Exception {
            double result = 0;
            if (NumberB == 0) {
                throw new Exception("除数不能为0。");
            }
            result = NumberA / NumberB;
            return result;
        }
    }

    /// <summary>
    /// 工厂方法
    /// </summary>
    interface IFactory {

        Operation CreateOperation();
    }



    /// <summary>
    /// 专门负责生产“-”的工厂
    /// </summary>
    class SubFactory implements IFactory {

        public Operation CreateOperation() {
            return new OperationSub();
        }
    }

    /// <summary>
    /// 专门负责生产“*”的工厂
    /// </summary>
    class MulFactory implements IFactory {

        public Operation CreateOperation() {
            return new OperationMul();
        }
    }

    /// <summary>
    /// 专门负责生产“/”的工厂
    /// </summary>
    class DivFactory implements IFactory {

        public Operation CreateOperation() {
            return new OperationDiv();
        }
    }
}
/// <summary>
/// 运算类
/// </summary>
class Operation {

    private double _numberA = 0;
    private double _numberB = 0;

    public double NumberA;

    public double NumberB;

    /// <summary>
    /// 得到运算结果
    /// </summary>
    /// <returns></returns>
    public double GetResult() throws Exception {
        double result = 0;
        return result;
    }
}
/// <summary>
/// 加法类
/// </summary>
class OperationAdd extends Operation {

    public double GetResult() {
        double result = 0;
        result = NumberA + NumberB;
        return result;
    }
}

/// <summary>
/// 专门负责生产“+”的工厂
/// </summary>
class AddFactory implements FactoryMethod.IFactory {

    public Operation CreateOperation() {
        return new OperationAdd();
    }
}